//
//  MOPhotoLibraryManager.m
//  WCImApp
//
//  Created by WCImApp on 2018/12/4.
//  Copyright © 2018年WCImApp. All rights reserved.
//

#import "MOPhotoLibraryManager.h"

@implementation MOPhotoLibraryManager

/**
 *  请求照片权限，注意，强烈要求用户获得照片权限，否则视频写入照片会有崩溃
 */
+ (void)requestALAssetsLibraryAuthorizationWithCompletion:(RequestAssetsLibraryAuthCompletion)requestAssetsLibraryAuthCompletion
{
    PHAuthorizationStatus authStatus = [PHPhotoLibrary authorizationStatus];
    if (authStatus != PHAuthorizationStatusAuthorized) // 未授权
    {
        [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
            if (status != PHAuthorizationStatusAuthorized)  //已授权
            {
                NSLog(@"用户拒绝访问相册！");
                if (requestAssetsLibraryAuthCompletion)
                {
                    requestAssetsLibraryAuthCompletion(NO);
                }
            }
            else
            {
                NSLog(@"用户允许访问相册！");
                if (requestAssetsLibraryAuthCompletion)
                {
                    requestAssetsLibraryAuthCompletion(YES);
                }
            }
        }];
    }
    else if (authStatus == PHAuthorizationStatusAuthorized)
    {
        NSLog(@"用户允许访问相册！");
        if (requestAssetsLibraryAuthCompletion)
        {
            requestAssetsLibraryAuthCompletion(YES);
        }
    }
    else if (authStatus == PHAuthorizationStatusDenied||authStatus ==PHAuthorizationStatusRestricted) {
        NSLog(@"用户拒绝访问相册！");
        if (requestAssetsLibraryAuthCompletion)
        {
            requestAssetsLibraryAuthCompletion(NO);
        }
    }
}

/**
 *  保存照片
 *
 *  @param image                UImage
 *  @param assetCollectionName  相册名字，不填默认为app名字+相册
 */
+ (void)savePhotoWithImage:(UIImage *)image andAssetCollectionName:(NSString *)assetCollectionName withCompletion:(SavePhotoCompletionBlock)savePhotoCompletionBlock
{
    if (assetCollectionName == nil || !assetCollectionName.length)
    {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        
        assetCollectionName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
        
        if (assetCollectionName == nil)
        {
            assetCollectionName = (@"吉信");
        }
    }
    
    __block NSString *blockAssetCollectionName = assetCollectionName;
    __block UIImage *blockImage = image;
    __block NSString *assetId = nil;

    
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

    // 这个block里保存一些"修改"性质的代码
    // 新建一个PHAssetCreationRequest对象, 保存图片到"相机胶卷"
    // 返回PHAsset(图片)的字符串标识
        assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:blockImage].placeholderForCreatedAsset.localIdentifier;

    } completionHandler:^(BOOL success, NSError * _Nullable error) {

        if (error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                savePhotoCompletionBlock(nil, error);
            });
            NSLog(@"保存图片到相机胶卷中失败");
            return;
        }
        NSLog(@"成功保存图片到相机胶卷中");
    // 2. 获得相册对象
        PHAssetCollection *collection = [self creatCollectionWithName:blockAssetCollectionName];
    // 3. 将“相机胶卷”中的图片添加到新的相册
        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

            PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:collection];
            // 根据唯一标示获得相片对象
            if (assetId) {
                PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
                // 添加图片到相册中
                if (asset) {
                    [request addAssets:@[asset]];
                }
            }
        } completionHandler:^(BOOL success, NSError * _Nullable error) {

            if (savePhotoCompletionBlock)
            {
                [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                    if (error) {
                        NSLog(@"保存照片失败!");
                        savePhotoCompletionBlock(nil, error);
                    } else {
                        NSLog(@"保存照片成功!");
                        savePhotoCompletionBlock(blockImage, nil);
                    }
                }];
            }
        }];

    }];

//    }
    
//    // 1. 存储图片到"相机胶卷"
//    [library performChanges:^{ // 这个block里保存一些"修改"性质的代码
//        // 新建一个PHAssetCreationRequest对象, 保存图片到"相机胶卷"
//        // 返回PHAsset(图片)的字符串标识
//        assetId = [PHAssetCreationRequest creationRequestForAssetFromImage:blockImage].placeholderForCreatedAsset.localIdentifier;
//    } completionHandler:^(BOOL success, NSError * _Nullable error) {
//        if (error) {
//            NSLog(@"error1%@", error);
//            return;
//        }
//
//        NSLog(@"成功保存图片到相机胶卷中");
//
//        // 2. 获得相册对象
//        // 获取曾经创建过的自定义视频相册名字
//        PHAssetCollection *createdAssetCollection = nil;
//        PHFetchResult <PHAssetCollection*> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
//        for (PHAssetCollection *assetCollection in assetCollections)
//        {
//            if ([assetCollection.localizedTitle isEqualToString:blockAssetCollectionName])
//            {
//                createdAssetCollection = assetCollection;
//                break;
//            }
//        }
//
//        //如果这个自定义框架没有创建过
//        if (createdAssetCollection == nil)
//        {
//            //创建新的[自定义的 Album](相簿\相册)
//            [library performChangesAndWait:^{
//
//                assetId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:blockAssetCollectionName].placeholderForCreatedAssetCollection.localIdentifier;
//
//            } error:&error];
//
//            NSLog(@"error2: %@", error);
//
//            //抓取刚创建完的视频相册对象
//            createdAssetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
//
//        }
//
//        // 3. 将“相机胶卷”中的图片添加到新的相册
////        [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
////            PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
////
////            // 根据唯一标示获得相片对象
////            PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil].firstObject;
////            // 添加图片到相册中
////            [request addAssets:@[asset]];
////        } completionHandler:^(BOOL success, NSError * _Nullable error) {
////            if (error)
////            {
////                NSLog(@"添加图片到相册中失败");
////                return;
////            }
////
////            NSLog(@"成功添加图片到相册中");
////        }];
//
//        // 将【Camera Roll】(相机胶卷)的视频 添加到【自定义Album】(相簿\相册)中
//        [library performChangesAndWait:^{
//            PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
//
//            [request addAssets:[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil]];
//
//        } error:&error];
//        NSLog(@"error3: %@", error);
//        // 提示信息
//        if (savePhotoCompletionBlock)
//        {
//            if (error)
//            {
//                NSLog(@"保存照片失败!");
//
//                savePhotoCompletionBlock(nil, error);
//            }
//            else
//            {
//                NSLog(@"保存照片成功!");
//
//                savePhotoCompletionBlock(blockImage, nil);
//            }
//        }
//
//    }];
}

+ (PHAssetCollection *)creatCollectionWithName:(NSString *)albName { // 先获得之前创建过的相册
    PHFetchResult*collectionResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

    for (PHAssetCollection *collection in collectionResult) {
        if ([collection.localizedTitle isEqualToString:albName]) {
            return collection;
        }
    }

    // 如果相册不存在,就创建新的相册(文件夹)

    __block NSString *collectionId = nil; // __block修改block外部的变量的值

    // 这个方法会在相册创建完毕后才会返回
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{

        // 新建一个PHAssertCollectionChangeRequest对象, 用来创建一个新的相册
        collectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:albName].placeholderForCreatedAssetCollection.localIdentifier;
    } error:nil];
    return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[collectionId] options:nil].firstObject;

}
/**
 *  保存视频
 *
 *  @param videoUrl             视频地址
 *  @param assetCollectionName  相册名字，不填默认为app名字+视频
 */
+ (void)saveVideoWithVideoUrl:(NSURL *)videoUrl andAssetCollectionName:(NSString *)assetCollectionName withCompletion:(SaveVideoCompletionBlock)saveVideoCompletionBlock
{
    if (assetCollectionName == nil)
    {
        NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
        
        assetCollectionName = [infoDictionary objectForKey:@"CFBundleDisplayName"];
        
        if (assetCollectionName == nil)
        {
            assetCollectionName = @"吉信相册";
        }
    }
    
    __block NSString *blockAssetCollectionName = assetCollectionName;
    __block NSURL *blockVideoUrl = videoUrl;
    PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        NSError *error = nil;
        __block NSString *assetId = nil;
        __block NSString *assetCollectionId = nil;
        
        // 保存视频到【Camera Roll】(相机胶卷)
        [library performChangesAndWait:^{
            
            assetId = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:blockVideoUrl].placeholderForCreatedAsset.localIdentifier;
            
        } error:&error];
        
        NSLog(@"error1: %@", error);
        
        // 获取曾经创建过的自定义视频相册名字
        PHAssetCollection *createdAssetCollection = nil;
        PHFetchResult <PHAssetCollection*> *assetCollections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
        for (PHAssetCollection *assetCollection in assetCollections)
        {
            if ([assetCollection.localizedTitle isEqualToString:blockAssetCollectionName])
            {
                createdAssetCollection = assetCollection;
                break;
            }
        }
        
        //如果这个自定义框架没有创建过
        if (createdAssetCollection == nil)
        {
            //创建新的[自定义的 Album](相簿\相册)
            [library performChangesAndWait:^{
                
                assetCollectionId = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:blockAssetCollectionName].placeholderForCreatedAssetCollection.localIdentifier;
                
            } error:&error];
            
            NSLog(@"error2: %@", error);
            
            //抓取刚创建完的视频相册对象
            if (assetCollectionId) {
                createdAssetCollection = [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[assetCollectionId] options:nil].firstObject;
            }
            
        }
        
        // 将【Camera Roll】(相机胶卷)的视频 添加到【自定义Album】(相簿\相册)中
        [library performChangesAndWait:^{
            PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdAssetCollection];
            if (assetId) {
                [request addAssets:[PHAsset fetchAssetsWithLocalIdentifiers:@[assetId] options:nil]];
            }
            
        } error:&error];
        NSLog(@"error3: %@", error);
        
        // 提示信息
        if (saveVideoCompletionBlock)
        {
            if (error)
            {
                NSLog(@"保存视频失败!");
                
                saveVideoCompletionBlock(nil, error);
            }
            else
            {
                NSLog(@"保存视频成功!");
                
                saveVideoCompletionBlock(blockVideoUrl, nil);
            }
        }
        
    });
}


@end
